package com.zyf.service.img;

import com.zyf.utils.ImageUtils;

import java.awt.Color;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.*;

public class AddTextToImg {

    /**
     * 按行进行写入图片文字的方法
     * @param imgPath
     * @param txtPath
     * @param flag
     * @param initLocation
     * @throws IOException
     */
    public static void getCompositeOfpicture(String imgPath, String txtPath, StringBuffer flag, int initLocation) throws IOException {
        System.out.println("开始添加文字----");
//        String smallPath = "https://i.picsum.photos/id/373/200/300.jpg?grayscale&hmac=K0qtT9ERX8kjHwHdhvkWwVuECjj1KNSDcBBpek-eeXY";
//        String outPath = uploadPath + "AAA" + System.currentTimeMillis() + ".png";

        /***测试贴图***/
//        ImageUtils.mergeImage(uploadPath, smallPath, outPath, "560", "242", 230, 230);
        /***测试字体  居中***/
        Font font = new Font("楷体", Font.BOLD, 35);   //定义字体
        Font font1 = new Font("楷体", Font.BOLD, 39);   //定义字体
        Font font2 = new Font("楷体", Font.BOLD, 32);   //定义字体


        Color color = new Color(6, 3, 3, 241);
        //字体
        File file = new File(txtPath);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String temp;
            int fontheight = 65;
            int row = 1;
            int num = initLocation;
            while ((temp = reader.readLine()) != null) {
                if (row < initLocation) {
                    row++;
                    continue;
                }
                if (temp.contains("***************")) {
                    flag.replace(0, flag.length(), num + 1 + "");
                    break;
                }
                if (num == initLocation) {
                    int res = ImageUtils.drawTextInImg(imgPath, temp.trim(), imgPath, font1, fontheight, color, 1000, 60, true, 1);
                } else if (num == initLocation + 1) {
                    int res = ImageUtils.drawTextInImg(imgPath, temp.trim(), imgPath, font2, fontheight, color, 1000, 60, true, 1);
                } else {
                    int res = ImageUtils.drawTextInImg(imgPath, temp.trim(), imgPath, font, fontheight, color, 1000, 60, true, 1);
                }
                num++;
                fontheight += 50;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        /**写多行字的时候需要计算y坐标**/
//        String text = "本地存放图片路径姓名定义";
//        int fontheight = 50;
//        int row =  ImageUtils.drawTextInImg(uploadPath, text, outPath, font, fontheight,  color, 240, 30, true, 0.9);
//
//        //这里需要计算下一行的y坐标  因为不确定上一行写了几行字
//        fontheight = fontheight + row * 30;
//        String text1 = "这里是测试第二行字体";
//         ImageUtils.drawTextInImg(outPath, text1, outPath, font, fontheight,  color, 240, 30, true, 0.9);

        /***测试贴图***/
        // ImageUtils.mergeImage(outPath2, uploadPath + "ewm.png", outPath5, "662", "985", 332, 332);
        System.out.println("------添加文字结束");

    }


    /**
     * 按全诗进行写入图片
     * @param imgPath
     * @param txtPath
     * @param flag
     * @param initLocation
     * @throws IOException
     */
    public static void handleAlltextImg(String imgPath, String txtPath, StringBuffer flag, int initLocation) throws Exception {
        System.out.println("开始添加文字----");
        /***测试字体  居中***/
        Font font = new Font("楷体", Font.BOLD, 35);   //定义字体
        Font font1 = new Font("楷体", Font.BOLD, 39);   //定义字体
        Font font2 = new Font("楷体", Font.BOLD, 32);   //定义字体
        Color color = new Color(6, 3, 3, 241);
        //字体
        File file = new File(txtPath);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String temp;
            int fontheight=50;
            int row = 1;
            int num = initLocation;
            //居中显示
            int textheight=getTextStartY( imgPath,  txtPath, fontheight,initLocation);

            while ((temp = reader.readLine()) != null) {
                if (row < initLocation) {
                    row++;
                    continue;
                }
                if (temp.contains("***************")) {
                    flag.replace(0, flag.length(), num + 1 + "");
                    break;
                }
                if(temp.length()>3){
                    int res = ImageUtils.drawTextInImg(imgPath, temp.trim(), imgPath, font, textheight, color, 1000, fontheight, true, 1);
                    textheight += 50;
                }
                num++;
                //分行写字体
//                if (num == initLocation) {
//                    int res = ImageUtils.drawTextInImg(imgPath, temp.trim(), imgPath, font1, textheight, color, 1000, fontheight, true, 1);
//                } else if (num == initLocation + 1) {
//                    int res = ImageUtils.drawTextInImg(imgPath, temp.trim(), imgPath, font2, textheight, color, 1000, fontheight, true, 1);
//                } else {
//                    int res = ImageUtils.drawTextInImg(imgPath, temp.trim(), imgPath, font, textheight, color, 1000, fontheight, true, 1);
//                }
//                num++;
//                textheight += 50;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("------添加文字结束");
    }

    /**
     * 根据传入参数添加文字
     * @param imgPath
     * @param txtPath
     * @param flag
     * @param initLocation
     * @throws Exception
     */
    public static void handleText2ImgByParam(String imgPath, String txtPath, StringBuffer flag, int initLocation) throws Exception {
        System.out.println("开始添加文字----");
        /***测试字体  居中***/
        Font font = new Font("楷体", Font.BOLD, 35);   //定义字体
        Color color = new Color(6, 3, 3, 241);
        //字体
        File file = new File(txtPath);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String temp;
            int fontheight=50;
            int row = 1;
            int num = initLocation;
            //居中显示
            int textheight=getTextStartY( imgPath,  txtPath, fontheight,initLocation);
            while ((temp = reader.readLine()) != null) {
                if (row < initLocation) {
                    row++;
                    continue;
                }
                if (temp.contains("***************")) {
                    flag.replace(0, flag.length(), num + 1 + "");
                    break;
                }
                if(temp.length()>3){
                    int res = ImageUtils.drawTextInImg(imgPath, temp.trim(), imgPath, font, textheight, color, 1000, fontheight, true, 1);
                }
                num++;
                textheight += 50;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("------添加文字结束");
    }

    /**
     * 获取文本的起始高度
     * @return
     */
    private static int getTextStartY(String imgPath, String txtPath,int fonttotalheight,int initLocation) {
        int textheight = 0;
        //字体
        File file = new File(txtPath);
        BufferedReader reader = null;
        int totalline=0;
        int num = initLocation;
        int row = 1;
        StringBuffer buffer = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String temp;
            //确定文字的起始高度
            while ((temp = reader.readLine()) != null) {
                if (row < initLocation) {
                    row++;
                    continue;
                }
                if (temp.contains("***************")) {
                    break;
                }
                if(temp.length()>3){
                    totalline ++;
                }
            }
            textheight=(totalline+1)*fonttotalheight;
            //图片加载到缓冲区
            BufferedImage bimage = ImageUtils.imageIoRead(imgPath);
            //图片的高
            int imgHerght = bimage.getHeight();
            //居中显示
            textheight = (imgHerght - textheight) / 2;
            return textheight;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }

}
